package red.service.impl;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.dao.UserDao;
import red.dao.impl.UserDaoImpl;
import red.mapper.Usermapper;
import red.model.ResModel;
import red.model.UserModel;
import red.service.UserService;
import red.util.Model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@SuppressWarnings("ALL")
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private Usermapper usermapper;
    private List<UserModel> list = new ArrayList<>();
    @Override
    public List<UserModel> selectuser(Model model) {
        UserModel userModel = new UserModel();
        return usermapper.selectuser(userModel);
    }

    @Override
    public List login(Model model) {
        String user = (String)model.get("user");
        String password = (String)model.get("password");
        UserModel userModel = new UserModel();
        userModel.setUser_name(user);
        userModel.setPassword(password);
        list = usermapper.loginuser(user,password);
        JSONObject object = new JSONObject();
        if(list.size()>0){
            usermapper.loginUser(userModel);
            object.put("msg","登录成功");
            object.put("data",1);
            return Collections.singletonList(object);
        }else {
            object.put("msg","登录失败");
            object.put("data",2);
            return  Collections.singletonList(object);
        }
    }

    @Override
    public List registuser(Model model) {
        String user = (String)model.get("user");
        String password = (String)model.get("password");
        UserModel userModel = new UserModel();
        userModel.setUser_name(user);
        userModel.setPassword(password);
        userModel.setUser_id(UUID.randomUUID().toString());
        list = usermapper.selectreg(user);
        JSONObject object = new JSONObject();
        if(list.size()>0){
            return null;
        }else {
            usermapper.registuser(userModel);
            object.put("msg","注册成功");
            object.put("data",1);
            return Collections.singletonList(object);
        }

    }

}
